package com.iwdnb.gkgz.common.prediction;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.Objects;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.bmnf.common.utils.BeanConvertUtils;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.model.dto.StockPredictionData;

public class DefaultPredictionHelper implements PredictionHelper{
    @Override
    public StockPredictionData predictionStock(StockDayData stockDayData) {
        StockPredictionData stockPredictionData = new StockPredictionData();
        stockPredictionData.setCode(stockDayData.getCode());
        //预测信息不准时,把预测信息设为当天的信息
        StockPredictionData prediction = null;
        if(Objects.nonNull(stockDayData.getTomorrowPrediction())) {
            prediction = BeanConvertUtils.convert(stockDayData.getTomorrowPrediction(),
                StockPredictionData.class);
        }else{
            prediction=new StockPredictionData();
            prediction.setCode(stockDayData.getCode());
            prediction.setDate(stockDayData.getDate());
            prediction.setMaxPrice(stockDayData.getMaxPrice());
            prediction.setMinPrice(stockDayData.getMinPrice());
        }
        //if (correctDays.contains(stockDayData.getDate())) {
        //    prediction = BeanConvertUtils.convert(stockDayData, StockPredictionData.class);
        //}
        if (prediction.getMaxPrice().compareTo(stockDayData.getMaxPrice()) > 0 || prediction.getMinPrice().compareTo(
            stockDayData.getMinPrice()) < 0) {
            prediction.setMaxPrice(stockDayData.getMaxPrice());
            prediction.setMinPrice(stockDayData.getMinPrice());
        }

        Date date = DateUtil.offsetDay(DateUtil.parseDate(stockDayData.getDate()), 1);
        stockPredictionData.setDate(DateUtil.formatDate(date));
        BigDecimal magicParam = new BigDecimal(2.718282);
        //(收盘-开盘)/2
        boolean upFlag = stockDayData.getOpenPrice().compareTo(stockDayData.getClosePrice()) <= 0;
        BigDecimal bigPrice = !upFlag ? stockDayData.getOpenPrice() : stockDayData.getClosePrice();
        BigDecimal smallPrice = !upFlag ? stockDayData.getClosePrice() : stockDayData.getOpenPrice();
        bigPrice = bigPrice.add(prediction.getMaxPrice()).add(stockDayData.getMaxPrice()).divide(new BigDecimal(3),
            BigDecimal.ROUND_HALF_DOWN);
        smallPrice = smallPrice.add(prediction.getMinPrice()).add(stockDayData.getMinPrice()).divide(new BigDecimal(3),
            BigDecimal.ROUND_HALF_DOWN);
        BigDecimal param = bigPrice.subtract(smallPrice).divide(magicParam, BigDecimal.ROUND_HALF_UP);
        //第二日(周、月)上涨空间预测=收盘+变量*n
        BigDecimal maxPrice = stockDayData.getClosePrice().add(param.multiply(new BigDecimal(1.5)))
            //.subtract(upFlag ? new BigDecimal(0.001) : new BigDecimal(0.001))
            .setScale(2, RoundingMode.HALF_DOWN);
        stockPredictionData.setMaxPrice(maxPrice);
        //第二日(周、月)下跌空间预测=收盘-变量*n
        BigDecimal minPrice = smallPrice.subtract(param.multiply(new BigDecimal(1.3)))
            .add(upFlag ? new BigDecimal(0.005) : new BigDecimal(0.001))
            .setScale(2, RoundingMode.HALF_UP);
        stockPredictionData.setMinPrice(minPrice);
        //maxPrice = stockPredictionData.getMaxPrice().subtract(new BigDecimal(0.005)).setScale(2,
        //    RoundingMode.HALF_DOWN);
        if (maxPrice.compareTo(stockPredictionData.getMinPrice()) <= 0) {
            minPrice = stockPredictionData.getMaxPrice().subtract(new BigDecimal(0.01)).setScale(2,
                RoundingMode.HALF_UP);
        }
        //stockPredictionData.setMaxPrice(maxPrice);
        stockPredictionData.setMinPrice(minPrice);
        return stockPredictionData;
    }
}
